package com.yang.flower.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.flower.common.Result;
import com.yang.flower.entity.Notice;
import com.yang.flower.mapper.NoticeMapper;
import com.yang.flower.service.NoticeService;
import com.yang.flower.utils.TokenUtils;
import jdk.nashorn.internal.parser.Token;
import org.apache.ibatis.annotations.Param;
import org.apache.tomcat.jni.Local;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description: TODO
 * @Author: Yang
 * @DateTime: 2023/2/12 17:15
 **/
@RestController
@CrossOrigin
@RequestMapping("/notice")
public class NoticeController {

    @Resource
    private NoticeMapper noticeMapper;

    @Autowired
    private NoticeService noticeService;

    @GetMapping("/pageAll")
    public Result pageAll(@RequestParam Integer pageNum,
                          @RequestParam Integer pageSize,
                          @RequestParam String name){
        Page<Notice> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null,Notice::getTitle,name).
                orderByDesc(Notice::getUpdateTime);
        Page<Notice> noticePage = noticeService.page(page, queryWrapper);
        return Result.success(noticePage);
    }

    @GetMapping("/page")
    public Result page(@RequestParam Integer pageNum,
                       @RequestParam Integer pageSize){
        Page<Notice> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Notice::getType,"活动公告");
        Page<Notice> noticePage = noticeService.page(page, queryWrapper);
        return Result.success(noticePage);
    }

    @GetMapping("/list/0")
    public Result list0(){
        List<Notice> noticeList = noticeMapper.list();
        return Result.success(noticeList);
    }

    @GetMapping("/list/1")
    public Result list1(@RequestParam Integer pageNum,
                        @RequestParam Integer pageSize){
        Page<Notice> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Notice::getCreateTime);
        Page<Notice> noticePage = noticeService.page(page, queryWrapper);
        return Result.success(noticePage);
    }

    @GetMapping("/listByNewTime")
    public Result listByNewTime(){
        List<Notice> noticeList = noticeMapper.listByNewTime();
        return Result.success(noticeList);
    }

    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable("id") Integer id){
        Notice notice = noticeMapper.getById(id);
        return Result.success(notice);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Notice notice){
        Long noticeId = notice.getId();
        if (noticeId == null) {
            notice.setCreateTime(LocalDateTime.now());
            notice.setCreateUser(TokenUtils.getCurrentUser().getUsername());
        }else{
            notice.setUpdateTime(LocalDateTime.now());
            notice.setCreateUser(TokenUtils.getCurrentUser().getUsername());
        }
        boolean save = noticeService.saveOrUpdate(notice);
        return Result.success(save);
    }

    @GetMapping("/getDiscountNotice")
    public Result getDiscountNotice(){
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Notice::getType,"活动公告");
        List<Notice> list = noticeService.list();
        return Result.success(list);
    }

    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable("id") Integer id){
        return Result.success(noticeService.removeById(id));
    }

}
